package com.example.printer2.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.printer2.adapter.po.FileViewHolder;
import com.example.printer2.R;
import com.example.printer2.po.MyFile;

import java.util.List;

public class FileListAdapter extends RecyclerView.Adapter<FileViewHolder> {
    List<MyFile> list;
    Handler handler;
    Context context;
    public FileListAdapter(List<MyFile> list,Handler handler,Context context)
    {
        this.context=context;
        this.list=list;
        this.handler=handler;
    }

    @Override
    public FileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FileViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull FileViewHolder holder, int position) {
        MyFile myFile=list.get(position);
        holder.file_name.setText(myFile.getFileName());
        holder.num.setText(String.valueOf(myFile.getNum()));
        holder.file_page.setText("页数："+String.valueOf(myFile.getPageNum()));
        if(myFile.getType()==0) {
            holder.merchant_avatar.setImageResource(R.mipmap.word);
        }
        else holder.merchant_avatar.setImageResource(R.mipmap.pdf);
        View.OnClickListener onClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId())
                {
                    case R.id.add:
                    {
                        holder.num.setText(String.valueOf(Integer.parseInt(holder.num.getText().toString())+1));
                        myFile.setNum(myFile.getNum()+1);
                        handler.sendEmptyMessage(0);
                        break;
                    }
                    case R.id.sub:
                    {
                        if(Integer.parseInt(holder.num.getText().toString())>1) {
                                holder.num.setText(String.valueOf(Integer.parseInt(holder.num.getText().toString()) - 1));
                                myFile.setNum(myFile.getNum() - 1);
                                handler.sendEmptyMessage(0);
                            }
                        break;
                    }
                    case R.id.del:
                    {
                        del(myFile);
                        break;
                    }
                }
            }
        };
        holder.add.setOnClickListener(onClickListener);
        holder.del.setOnClickListener(onClickListener);
        holder.sub.setOnClickListener(onClickListener);
    }



    public void add(MyFile m) {
        list.add(m);
        notifyItemInserted(list.size());
        handler.sendEmptyMessage(0);
    }

    public void del(MyFile m) {
        list.remove(m);
        notifyItemInserted(list.size());
        handler.sendEmptyMessage(0);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
